package com.zry.simpleBlog.controller.admin;


import com.zry.simpleBlog.comment.aop.annotations.AuthCheck;
import com.zry.simpleBlog.comment.aop.annotations.LogWeb;
import com.zry.simpleBlog.comment.enums.RespBeanEnum;
import com.zry.simpleBlog.comment.respBean.RespBean;
import com.zry.simpleBlog.comment.utils.CookieUtil;
import com.zry.simpleBlog.comment.utils.UserContext;
import com.zry.simpleBlog.dto.LoginDTO;
import com.zry.simpleBlog.dto.UpdatePswDTO;
import com.zry.simpleBlog.dto.UserDTO;
import com.zry.simpleBlog.entity.User;
import com.zry.simpleBlog.service.IUserService;
import com.zry.simpleBlog.service.RedisService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


/**
 * @author zry
 * @since 2022-04-09
 */
@RestController
@Tag(name = "用户信息接口")
public class AdminUserController {
    @Resource
    private IUserService userService;

    @Resource
    private RedisService redisService;

    @Value("${cookie.user.key}")
    private String cookieKey;
    @Value("${cookie.user.surviveTime}")
    private long surviveTime;

    /**
     * 功能描述: 登录
     *
     * @author zry
     */
    @Operation(summary = "用户登录")
    @PostMapping("login")
    @LogWeb(title = "后台管理", action = "用户登录")
    public RespBean login(@RequestBody @Valid LoginDTO loginDto) {
        return userService.doLogin(loginDto);
    }

    /**
     * 功能描述: 通过 cookie 中的 token 获取用户信息
     *
     * @author zry
     */
    @Operation(summary = "目前登录用户信息")
    @GetMapping("userInfoByTicket")
    @AuthCheck
    @LogWeb(title = "用户操作", action = "redis中获取用户信息")
    public RespBean userInfo() {
        User user = UserContext.getCurrentUser();
        return RespBean.success(UserDTO.valueOf(user));
    }

    /**
     * 功能描述: 更新用户信息
     *
     * @author zry
     */
    @Operation(summary = "更新用户信息", description = "暂时不包括密码")
    @PutMapping("user/{id}")
    @AuthCheck
    @LogWeb(title = "用户操作", action = "更新用户信息")
    public RespBean updateUserInfo(@Valid @RequestBody UserDTO userDto, @PathVariable String id, HttpServletRequest request) {
        userDto.setId(id);
        //更新数据库中的用户信息
        userService.updateById(userDto.castUser());
        String ticket = CookieUtil.getCookieValue(request, cookieKey);
        User user = userService.getById(id);
        user.setSalt("你也不知道");
        user.setPassword("你也不知道");
        redisService.saveUser(ticket, user, surviveTime);
        return RespBean.success(RespBeanEnum.UPDATE_SUCCESS);
    }

    /**
     * 功能描述: 更新用户密码
     *
     * @author zry
     */
    @Operation(summary = "更新用户密码", description = "更新密码")
    @PostMapping("updatePassword")
    @AuthCheck
    @LogWeb(title = "用户操作", action = "更新密码")
    public RespBean updatePassword(@Valid @RequestBody UpdatePswDTO pswDto, HttpServletRequest request) {
        return userService.updatePassword(pswDto, request);
    }


    /**
     * 功能描述: 退出登录
     *
     * @author zry
     */
    @Operation(summary = "退出登录", description = "需要cookie中的key-value,将redis中的用户信息删除")
    @PostMapping("logout")
    @AuthCheck
    @LogWeb(title = "用户操作", action = "退出登录")
    public RespBean updateUserInfo(HttpServletRequest request,
                                   HttpServletResponse response) {
        //1.删除redis中的user信息
        redisService.deleteUser(CookieUtil.getCookieValue(request, cookieKey));
        //2.删除cookie 中的user Token
        CookieUtil.deleteCookie(request, response, cookieKey);
        return RespBean.success();
    }

}
